//
//  YFMenuView.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/1/30.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "YFMenuView.h"

@implementation YFMenuView

- (instancetype)initWithFrame:(CGRect)frame select:(NSInteger)index
{
   self = [super initWithFrame:frame];
    if (self) {
        _selectIndex = index;
        [self addSubViews];
        self.backgroundColor = kColors(255, 255, 255, 1);
    }
    return self;
}

- (void)addSubViews
{
    self.btnArr = [NSMutableArray array];
    NSArray *btnTitles = @[@"全部", @"未付款", @"已付款", @"待晒单"];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        if (i == 0) {
          button.frame = CGRectMake(55*kWidthScale + ((kScreenWidth-42 )/3) * i, 0, 42, self.frame.size.height);
        }
        else if(i == 1)
        {
            button.frame = CGRectMake(55*kWidthScale + ((kScreenWidth-42 )/3) * i, 0, 0, self.frame.size.height);
            
        }else
        {
         button.frame = CGRectMake(55*kWidthScale + ((kScreenWidth-42 )/3) * (i-1), 0, 42, self.frame.size.height);
        }
        
        
        UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(button.left, 15, button.width, 31)];
        [button setTitle:btnTitles[i] forState:(UIControlStateNormal)];
        [button setTitleColor:kColors(102, 102, 102, 1) forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.backgroundColor = kColors(255, 255, 255, 1);
        bgV.backgroundColor = kColors(218, 166, 46, 1);
        bgV.hidden = YES;
        bgV.tag = 100 + i;
        [self.btnArr addObject:button];
        button.tag = 10000 + i;
        [button addTarget:self action:@selector(actionChangeMenu:) forControlEvents:(UIControlEventTouchUpInside)];
        if (i == _selectIndex) {
            [button setTitleColor:kColors(218, 166, 46, 1) forState:(UIControlStateNormal)];
            bgV.hidden = NO;
        }
        [self addSubview:bgV];
        [self addSubview:button];
        [self bringSubviewToFront:button];
    }
}
    
- (void)actionChangeMenu:(UIButton *)button
{
    self.selectIndex = button.tag - 10000;
    [button setTitleColor:kColors(218, 166, 46, 1) forState:(UIControlStateNormal)];
    
    for (UIView *v in self.subviews) {
        if (v.tag == self.selectIndex + 100) {
            v.hidden = NO;
        } else if (![v isKindOfClass:[UIButton class]]) {
            v.hidden = YES;
        } else if ([v isKindOfClass:[UIButton class]] && v != button) {
            UIButton * btn = (UIButton *)v;
            [btn setTitleColor:kColors(102, 102, 102, 1) forState:(UIControlStateNormal)];
        }
    }
    [self.delegate changeMenuAtIndex:self.selectIndex];
}





@end
